<template>
  <div class="register">
     <van-uploader :after-read="afterRead" />
      <van-image
  round
  width="5rem"
  height="5rem"
  :src=imgurl
  v-if="imgurl"
/>
    <van-form @submit="onSubmit">
      <van-field
        v-model="nickName"
        name="nickName"
        label="用户昵称"
        placeholder="用户昵称"
        :rules="[{ required: true, message: '请填写用户昵称' }]"
      />
      <van-field
        v-model="username"
        name="userName"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
          <van-button class="resigt" round block type="info" @click="gologin">已注册？点击跳转登陆页面</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from "vant";
import {goregister} from "../../api/user";
export default {
  data() {
    return {
      nickName:"",
      username: '',
      password: '',
      imgurl: "",
    };
  },
  computed: {},
  watch: {},

   methods: {
     //点击跳转登陆页面
     gologin(){
     this.$router.push("/login")
     },
    async onSubmit(values) {
      console.log('submit', values);
    //   console.log(values);
    //   console.log({...values});
      const result=await goregister({...values,avatar: this.imgurl });
      console.log(result);
      if(result.code==="success"){
         Toast("注册成功");
          this.$router.push("/login");
      }
    },
    //上传头像
    afterRead(file) {
      // 此时可以自行将文件上传至服务器
      console.log(file);
      this.imgurl = file.content;
    },
  },
  created() {},
  mounted() {
      
  },
  components: {},
};
</script>
<style scoped>
img{
  width: 100px;
  height: 100px;
}
.resigt{
  margin-top: 10px;
}
</style>
